﻿/// <reference path="_namespace.js" />
/// <reference path="geometry.js" />

(function () {

    var Mylocation = {
        latitude: 47.6097,  // decimal degrees
        longitude: 122.3331 // decimal degrees
    };

    var maximumDistanceInMilesFromConferenceToShowVenue = 10;

    var distanceElement = document.getElementById("distance");
    var travelSection = document.querySelector("section.travel");
    var venueSection = document.querySelector("section.venue");

    var distanceFromConference = function (coords) {
        return Math.floor(WebApp2013.geometry.distanceInMiles(coords, Mylocation));
    };

    var showDistanceMessage = function (distance) {
        var message = "You are " + distance + " miles from the WebApp2013";
        distanceElement.textContent = message;
    };

    var moveVenueSectionToTop = function () {
        travelSection.parentNode.insertBefore(venueSection, travelSection);
    };

    var updateUIForPosition = function (position) {
        var distance = distanceFromConference(position.coords);
        showDistanceMessage(distance);
        var isNearToConference = distance < maximumDistanceInMilesFromConferenceToShowVenue;
        if (isNearToConference) {
            moveVenueSectionToTop();
        }
    };

    var error = function () {
        distanceElement.textContent = "Could not detect your current location.";
    };

    navigator.geolocation.getCurrentPosition(updateUIForPosition, error);

} ());
